package com.whw.fy.service.impl;

import com.whw.fy.entity.*;
import com.whw.fy.mapper.*;
import com.whw.fy.utils.AgeUtil;
import com.whw.fy.utils.GetEveryDate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import com.whw.fy.service.PurchaseCourseRecordsService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class PurchaseCourseRecordsServiceImpl implements PurchaseCourseRecordsService {

    @Resource
    private PurchaseCourseRecordsMapper purchaseCourseRecordsMapper;
    @Resource
    private LessonMapper lessonMapper;
    @Resource
    private ClazzMapper clazzMapper;
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private StaffMapper staffMapper;

    @Override
    public Message deleteByPrimaryKey(Integer purchaseId) {
        return purchaseCourseRecordsMapper.deleteByPrimaryKey(purchaseId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insert(PurchaseCourseRecords record) {
        return purchaseCourseRecordsMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(PurchaseCourseRecords record) {
        return purchaseCourseRecordsMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer purchaseId) {
        PurchaseCourseRecords purchaseCourseRecords = purchaseCourseRecordsMapper.selectByPrimaryKey(purchaseId);
        //设置课程类型
        Clazz clazz = clazzMapper.selectByPrimaryKey(purchaseCourseRecords.getPurchaseClazzId());
        System.out.println(clazz);
        if (clazz != null) {
            if (clazz.getClazz_type().getClazzTypeName() != null) {
                purchaseCourseRecords.setPurchaseClazzTypeName(clazz.getClazz_type().getClazzTypeName());
            }
            //设置授课老师名称
            purchaseCourseRecords.setPurchasePeopleTeacherName(staffMapper.selectByPrimaryKey(clazz.getClassTeacherId()).getStaffName());
            //设置主责老师名称
            purchaseCourseRecords.setPurchasePrincipalTeacherName(staffMapper.selectByPrimaryKey(clazz.getClassResponsibleTeacherId()).getStaffName());
            //设置教学主管名称
            purchaseCourseRecords.setPurchaseTeachingDirectorName(staffMapper.selectByPrimaryKey(clazz.getClassTeachingDirectorId()).getStaffName());
            //设置医疗主管名称
            purchaseCourseRecords.setPurchaseMedicalDirectorName(staffMapper.selectByPrimaryKey(clazz.getClassMedicalDirectorId()).getStaffName());
        }
        //设置技术类别
        Lesson lesson = lessonMapper.selectByPrimaryKey(purchaseCourseRecords.getClazz().getClassCourseId());
        if (lesson!=null){
            if (lesson.getTechnicalType().getTechnicalTypeName()!=null){
                purchaseCourseRecords.setPurchasetechnicalTypeName(lesson.getTechnicalType().getTechnicalTypeName());
            }
        }
        return Message.success(null).add(purchaseCourseRecords);
    }

    @Override
    public Message updateByPrimaryKeySelective(PurchaseCourseRecords record) {
        return purchaseCourseRecordsMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(PurchaseCourseRecords record) {
        return purchaseCourseRecordsMapper.updateByPrimaryKey(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public int batchInsert(List<PurchaseCourseRecords> list) {
        return purchaseCourseRecordsMapper.batchInsert(list);
    }

    @Override
    public Message selectAll() {
        List<PurchaseCourseRecords> purchaseCourseRecordsList = purchaseCourseRecordsMapper.selectAll();
        return Message.success(null).add(initPurchaseCourseRecordsList(purchaseCourseRecordsList));
    }

    @Override
    public Message selectByStudentId(Integer studentId) {
        return Message.success(null).add(purchaseCourseRecordsMapper.selectByStudentId(studentId));
    }

    @Override
    public Message selectByStudentIdAndClassStatus(Integer studentId, Integer classStatus) {
        return Message.success(null).add(purchaseCourseRecordsMapper.selectByStudentIdAndClassStatus(studentId, classStatus));
    }

    @Override
    public int updateBatchSelective(List<PurchaseCourseRecords> list) {
        return purchaseCourseRecordsMapper.updateBatchSelective(list);
    }

    //对从数据库查询到的studentList进行封装
    private List<PurchaseCourseRecords> initPurchaseCourseRecordsList(List<PurchaseCourseRecords> purchaseCourseRecordsList) {
        for (PurchaseCourseRecords purchaseCourseRecords : purchaseCourseRecordsList) {
            //设置课程类型
            Clazz clazz = clazzMapper.selectByPrimaryKey(purchaseCourseRecords.getPurchaseClazzId());
            System.out.println(clazz);
            if (clazz != null) {
                if (clazz.getClazz_type().getClazzTypeName() != null) {
                    purchaseCourseRecords.setPurchaseClazzTypeName(clazz.getClazz_type().getClazzTypeName());
                }
                //设置授课老师名称
                purchaseCourseRecords.setPurchasePeopleTeacherName(staffMapper.selectByPrimaryKey(clazz.getClassTeacherId()).getStaffName());
                //设置主责老师名称
                purchaseCourseRecords.setPurchasePrincipalTeacherName(staffMapper.selectByPrimaryKey(clazz.getClassResponsibleTeacherId()).getStaffName());
                //设置教学主管名称
                purchaseCourseRecords.setPurchaseTeachingDirectorName(staffMapper.selectByPrimaryKey(clazz.getClassTeachingDirectorId()).getStaffName());
                //设置医疗主管名称
                purchaseCourseRecords.setPurchaseMedicalDirectorName(staffMapper.selectByPrimaryKey(clazz.getClassMedicalDirectorId()).getStaffName());
            }
            //设置技术类别
            Lesson lesson = lessonMapper.selectByPrimaryKey(purchaseCourseRecords.getClazz().getClassCourseId());
            if (lesson!=null){
                if (lesson.getTechnicalType().getTechnicalTypeName()!=null){
                    purchaseCourseRecords.setPurchasetechnicalTypeName(lesson.getTechnicalType().getTechnicalTypeName());
                }
            }
        }
        return purchaseCourseRecordsList;
    }
}








